package jdk;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2021-09-14 10:19
 **/
public class Flyweight {
    public static void main(String[] args) {


        // 常量池
        String s1 = "hello";
        String s2 = new String("hello");
        String s3 = s2.intern();
        String s4 = "hello";
        System.out.println(s1 == s2);
        System.out.println(s1 == s3);

        System.out.println("--------------------");

        // 对于 Integer var = ? 在-128 至 127 之间的赋值，Integer 对象是在 IntegerCache.cache 产生，会复用已有对象

        /*
        public static Integer valueOf(int i) {
            if (i >= Integer.IntegerCache.low && i <= Integer.IntegerCache.high)
                return Integer.IntegerCache.cache[i + (-Integer.IntegerCache.low)];
            return new Integer(i);
        }
        */
        Integer x = 127;
        Integer y = new Integer(127);
        Integer z = 127;
        Integer w = new Integer(127);

        System.out.println(x.equals(y));
        System.out.println(x == y);
        System.out.println(x == z);
        System.out.println(y == w);
    }
}
